package com.example.admin.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.admin.mapper.BlogMapper;
import com.example.admin.mapper.CategoryMapper;
import com.example.admin.service.BlogService;
import com.example.xo.pojo.Blog;
import com.example.xo.pojo.Category;
import com.example.xo.vo.BlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Override
    public boolean add(Blog blog) {
        String id = IdUtil.getSnowflakeNextIdStr();
        blog.setBlogId(id);
        Blog blogInDb = findBlogByTitle(blog.getTitle());
        if (blogInDb!=null){
            return false;
        }

        return blogMapper.insert(blog)==1;
    }

    @Override
    public BlogVo findByTitle(String title) {
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getTitle,title);
        Blog blog = blogMapper.selectOne(wrapper);
        if (blog==null){
            return null;
        }

        return blogVoId(blog);
    }

    @Override
    public List<BlogVo> findAll() {
        List<BlogVo> vos=new ArrayList<>();
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        List<Blog> blogs=blogMapper.selectList(wrapper);
        blogMapper.selectList(wrapper);
        for (Blog blog : blogs) {
            BlogVo vo = blogVoId(blog);
            vos.add(vo);
        }
        return vos;
    }

    public BlogVo blogVoId(Blog blog){
        Category category = categoryMapper.selectById(blog.getCategoryId());
        BlogVo vo=new BlogVo();
        vo.setBlogId(blog.getBlogId());
        vo.setAuthor(blog.getAuthor());
        vo.setCategoryName(category.getCategoryName());
        vo.setTitle(blog.getTitle());
        vo.setCreated(blog.getCreated());
        return vo;
    }

    @Override
    public BlogVo findById(String id) {
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId,id);
        Blog blog = blogMapper.selectOne(wrapper);
        if (blog==null){
            return null;
        }

        return blogVoId(blog);
    }

    @Override
    public boolean deleteById(String id) {
        Blog blog = findBlogById(id);
        if (blog==null){
            return false;
        }
        blog.setDeleted(LocalDateTime.now());
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId,id);
        int res = blogMapper.update(blog, wrapper);
        return res==1;
    }

    @Override
    public boolean update(Blog blog) {
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId,blog.getBlogId());
        wrapper.isNull(Blog::getDeleted);
        //更新时间
        blog.setUpdated(LocalDateTime.now());
        //判断标题是否已经重复了
        BlogVo blogInDb = findByTitle(blog.getTitle());
        //数据中的记录要排除自己
        if(blogInDb!=null&&!blog.getBlogId().equals(blogInDb.getBlogId())){
            return false;
        }
        //去掉对作者的修改
        blog.setAuthor(null);

        int res = blogMapper.update(blog, wrapper);
        return res>0;
    }

    @Override
    public Blog findBlogById(String id) {
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId,id);
        return blogMapper.selectOne(wrapper);
    }

    @Override
    public Blog findPoById(String id) {
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId,id);
        return blogMapper.selectOne(wrapper);
    }

    public Blog findBlogByTitle(String title) {
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle,title);
        Blog blog = blogMapper.selectOne(wrapper);

        return blog;
    }


}
